package  
{
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Spritemap;
	import net.flashpunk.FP;
	/**
	 * © 2012 TheJavaArcade.com
	 */	
	public class Butterfly extends Entity {
		private var butterflySprite:Spritemap = new Spritemap(Assets.BUTTERFLY_SPRITE, 16, 16);
		public var level:int;
		public var state:int;
		
		public function Butterfly(theLevel:int) {
			level = theLevel;
			state = 0;
			butterflySprite.add("fly", [0,1,2,1], 20, true);
			graphic = butterflySprite;
			butterflySprite.play("fly");
			type = "butterfly";
			setHitbox(48, 48, 16, 16);
			layer = 1;
			var dataList:XMLList;
			var dataElement:XML;
			dataList = GameEngine.levelXML[theLevel].bugs.butterfly;
			for each(dataElement in dataList) {
				x = dataElement.@x;
				y = dataElement.@y;
			}
		}
		override public function update():void {
			if (GameEngine.level == level && collide("fracas", x , y)) {
				if (state == 0) {
					GameEngine.butterflyDialogue.theText.text = "I'm a double agent working for the worms.  Shhh.";
					GameEngine.butterflyDialogue.y = y - 25;
					GameEngine.butterflyDialogue.x = (x +8) - (GameEngine.butterflyDialogue.theText.width / 2);
				}
				if (state == 1) {
					GameEngine.butterflyDialogue.theText.align = "left";
					GameEngine.butterflyDialogue.theText.text = "   Secret plans? F@#k yeah!\n   Please, take this reward.\n Hold Z when falling to use it.";
					GameEngine.butterflyDialogue.y = y - 32;
					GameEngine.file.state = 3;
					GameEngine.worm.state = 3;
					GameEngine.umbrella.state = 1;
					GameEngine.umbrella.x = x - 55;
					GameEngine.umbrella.y = y;
					GameEngine.umbrella.visible = true;
					GameEngine.butterflyDialogue.x = (x +8) - (GameEngine.butterflyDialogue.theText.width / 2)+40;
				}
				if (state == 2) {
					GameEngine.butterflyDialogue.theText.text = "Thank you. Now go away.";
					GameEngine.butterflyDialogue.theText.align = "center";
					GameEngine.butterflyDialogue.y = y - 25;
					GameEngine.butterflyDialogue.x = (x +8) - (GameEngine.butterflyDialogue.theText.width / 2);
				}
				
				GameEngine.butterflyDialogue.visible = true;
			}else {
				GameEngine.butterflyDialogue.visible = false
			}
		}		
	}

}